/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * calculator_gui.java
 *
 * Created on 20 Apr, 2011, 9:21:29 AM
 */
package rdf_application_2;

import calculator.*;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pacy
 */
public class calculator_gui extends javax.swing.JFrame {

    /** Creates new form calculator_gui */
    public calculator_gui() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        closeCalButton = new javax.swing.JButton();
        prefixExp = new javax.swing.JTextField();
        postfixExp = new javax.swing.JTextField();
        outputExp = new javax.swing.JTextField();
        inputExp = new javax.swing.JTextField();
        parseExp = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(rdf_application_2.RDF_Application_2.class).getContext().getResourceMap(calculator_gui.class);
        closeCalButton.setText(resourceMap.getString("closeCalButton.text")); // NOI18N
        closeCalButton.setName("closeCalButton"); // NOI18N
        closeCalButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeCalButtonActionPerformed(evt);
            }
        });

        prefixExp.setName("prefixExp"); // NOI18N
        prefixExp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prefixExpActionPerformed(evt);
            }
        });

        postfixExp.setName("postfixExp"); // NOI18N
        postfixExp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                postfixExpActionPerformed(evt);
            }
        });

        outputExp.setName("outputExp"); // NOI18N
        outputExp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                outputExpActionPerformed(evt);
            }
        });

        inputExp.setName("inputExp"); // NOI18N
        inputExp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                inputExpActionPerformed(evt);
            }
        });

        parseExp.setText(resourceMap.getString("parseExp.text")); // NOI18N
        parseExp.setName("parseExp"); // NOI18N
        parseExp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parseExpActionPerformed(evt);
            }
        });

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(prefixExp, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(postfixExp, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(outputExp, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(closeCalButton)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(inputExp, javax.swing.GroupLayout.PREFERRED_SIZE, 270, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(28, 28, 28)
                            .addComponent(parseExp, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(inputExp, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(parseExp, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(outputExp))
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(postfixExp, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 20, Short.MAX_VALUE)
                    .addComponent(prefixExp))
                .addGap(41, 41, 41)
                .addComponent(closeCalButton)
                .addGap(21, 21, 21))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void prefixExpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prefixExpActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_prefixExpActionPerformed

    private void postfixExpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_postfixExpActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_postfixExpActionPerformed

    private void outputExpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_outputExpActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_outputExpActionPerformed

    private void inputExpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputExpActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_inputExpActionPerformed

    private void parseExpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parseExpActionPerformed
        // TODO add your handling code here:

        try {

            if (!inputExp.getText().endsWith(";")) {
                inputExp.setText(inputExp.getText() + ";");
            }
            InputStream is = new ByteArrayInputStream(inputExp.getText().getBytes("UTF-8"));
            InputStream is1 = new ByteArrayInputStream(inputExp.getText().getBytes("UTF-8"));
            InputStream is2 = new ByteArrayInputStream(inputExp.getText().getBytes("UTF-8"));


            Lexer lexer = new Lexer(is);
            Lexer lexer1 = new Lexer(is1);
            Lexer lexer2 = new Lexer(is2);

            outputExp.setText(calc(lexer));
            postfixExp.setText(postFix(lexer1));
            prefixExp.setText(preFix(lexer2));
        } catch (Exception e) {
        }
    }//GEN-LAST:event_parseExpActionPerformed

    public int doAddition(int op1, int op2) {
        try {
            InputStream is = new ByteArrayInputStream((op1 + "+" + op2 + ";").getBytes("UTF-8"));
            Lexer lexer = new Lexer(is);
            return Integer.valueOf(calc(lexer));
        } catch (Exception ex) {
            Logger.getLogger(calculator_gui.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    private void closeCalButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeCalButtonActionPerformed
        dispose();
    }//GEN-LAST:event_closeCalButtonActionPerformed

    private String calc(Lexer lexer) throws Exception {
        CalcParser calcParser = new CalcParser(lexer);
        calcParser.parse();
        return calcParser.finalValue;
    }

    private String postFix(Lexer lexer) throws Exception {
        PostfixParser postfixParser = new PostfixParser(lexer);
        postfixParser.parse();
        return postfixParser.finalValue;
    }

    private String preFix(Lexer lexer) throws Exception {
        PrefixParser prefixParser = new PrefixParser(lexer);
        prefixParser.parse();
        return prefixParser.finalValue;
    }

    /**
     * @param args the command line arguments
     */
    public static void run_calc_gui() {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new calculator_gui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeCalButton;
    private javax.swing.JTextField inputExp;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField outputExp;
    private javax.swing.JButton parseExp;
    private javax.swing.JTextField postfixExp;
    private javax.swing.JTextField prefixExp;
    // End of variables declaration//GEN-END:variables
}
